<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Gen extends MX_Controller {

	function __construct() {
		parent::__construct();

		$this->load->database();
		$this->load->helper('url');
		$this->load->model('gen_model', 'm', true);
	}

	function index() {
		$result = $this->m->getInfomation();
		$data = array();
		foreach ($result as $value) {
			$data[$value['TABLE_NAME']][] = $value['COLUMN_NAME'];
		}
		$this->load->view('crud', array('data' => $data));
	}

	function generate() {
		$result = $this->m->getInfomation();
		$data = array();

		foreach ($result as $value) {
			$data[$value['TABLE_NAME']][] = $value['COLUMN_NAME'];
		}
		$aryTable = array_unique(array_keys($data)) ;
		$str = '';
		foreach ($aryTable as $table) {			
			$str .= "\t".'function '.$table.' () {'."\n";
			$str .= "\t\t".'$crud = new grocery_CRUD();' . "\n";
			//$crud->set_table('tblsinhvien');
			$str .= "\t\t".'$crud->set_table("' . $table . '");'. "\n";
			//for column
			$aryShowColumn = array();
			$arydisplayColumn = array();
			$strRelation = '';
			$strChangeField = '';
			$aryEdit = array();
			$aryAdd = array();
			$aryHiddenFunction = array();
			foreach ($data[$table] as $column) {
				//1. Xu ly display name
				if(isset($_REQUEST[$table.'_'.$column.'_display_name']) && strlen($_REQUEST[$table.'_'.$column.'_display_name']) > 0) {
					$arydisplayColumn[$column] = $_REQUEST[$table.'_'.$column.'_display_name'];
				}
				//2. Xu ly show
				if(isset($_REQUEST[$table.'_'.$column.'_show']) && $_REQUEST[$table.'_'.$column.'_show']) {
					$aryShowColumn[] = $column;
				}
				//3. Xu ly edit
				if(isset($_REQUEST[$table.'_'.$column.'_edit']) && $_REQUEST[$table.'_'.$column.'_edit']) {
					$aryEdit[] = $column;
				}

				//xy ly add
				if(isset($_REQUEST[$table.'_'.$column.'_add']) && $_REQUEST[$table.'_'.$column.'_add']) {
					$aryAdd[] = $column;
				}
				//4. Xu ly relation
				if(isset($_REQUEST[$table.'_'.$column.'_relation'])
					&& isset($_REQUEST[$table.'_'.$column.'_relation_id'])
					&& isset($_REQUEST[$table.'_'.$column.'_relation_display'])
					&& strlen($_REQUEST[$table.'_'.$column.'_relation']) > 0 
					&& strlen($_REQUEST[$table.'_'.$column.'_relation_id']) > 0 
					&& strlen($_REQUEST[$table.'_'.$column.'_relation_display']) > 0 ) {

					$id = $_REQUEST[$table.'_'.$column.'_relation_id'];
					$display = $_REQUEST[$table.'_'.$column.'_relation_display'];
					$tableRelation = $_REQUEST[$table.'_'.$column.'_relation'];
					$strRelation .= "\t\t".'$crud->set_relation("' . $id .'", "' . $tableRelation . '", "{' . $display .'}");' . "\n";
				}
				//5. Xu ly hidden function
				if(isset($_REQUEST[$table.'_'.$column.'_function'] )) {
					$type = $_REQUEST[$table.'_'.$column.'_function'];
					if($type == 'time()' || $type == "date('Y-m-d H:i:s')") {
						$strChangeField .= "\t\t".'$crud->change_field_type("'. $column .'", "hidden", '. $type .');' . "\n";
					} elseif($type == 'password') {
						$strChangeField .= "\t\t".'$crud->change_field_type("'. $column .'", "password");';
					}
				}
				
			}
			//show column
			$str .= "\t\t".'$crud->columns("'. join('","', $aryShowColumn) .'");'."\n";

			//display as
			$tmp = '$crud';
			foreach ($arydisplayColumn as $key => $value) {
				$tmp .= '->display_as("'. $key .'","'. $value .'")' ."\n\t\t\t";
			}
			$str .= "\t\t".trim($tmp) . ";\n";

			//relation
			$str .= $strRelation;

			//edit field
			$str .= "\t\t".'$crud->edit_fields("'. join('","', $aryEdit) .'");'."\n";

			//change field type
			$str .= $strChangeField;

			//add field
			$str .= "\t\t".'$crud->add_fields("'. join('","', $aryAdd) .'");'."\n";			

			//subject
			$str .= "\t\t".'$crud->set_subject("' . $table . '");'. "\n";

			//common
			$str .= "\t\t".'$output = $crud->render();' . "\n";
			$str .= "\t\t".'$output->title = "'. $table .'";'. "\n";
			$str .= "\t\t".'$this->_view_output($output);'."\n";
			$str .= "\t}\n\n";
		}
		$controller = file_get_contents('assets/gencode/file_admin_gen_template.php');
		$search = array('{function}');
		$replace = array($str); 
		$c_content = str_replace($search, $replace, $controller);
		file_put_contents('assets/gencode/admin.php', $c_content);
		die('done');
	}

	function file() {

	}

}